<?php
if (!defined('IN_XLP')) {
    exit('Access Denied');
}
$Document = array(
    'pageid' => 'index-index', //页面标示
    'pagename' => '', //当前页面名称
    'mycss' => array('index/common','index/style','index/fetch'), //加载的css样式表
    'myjs' => array('global/jquery-1.9.0.min','global/layer/layer','global/jquery.cookie'), //加载的js脚本
    'footerjs'=>array(),
    'head'=>true//是否加载头部文件
);
include getTpl('header', 'public');
?>
<style type="text/css">
    .info_one .server i{ margin-left: 5px; background: none;}
    .info_one .server .ji {background: #ff8400}
    .info_one .server .ling {background: #0096ff}
    .info_one .server .bao {background: #72b601}
    .info_one .server .shang {background: #de00ff}
    .info_one .uu{ color: #414141 }
    .info_one .dd{ color: #72b601 }
    .info_one .uoo{ color: #0096ff }
    .info_one .ee{ color: #ea11a4 }
    .hide{display: none;}
</style>
<body>
<?php include getTpl('small_top' , 'public'); ?>
<?php include getTpl('top' , 'public'); ?>
<div class="content">
    <!-- 广告位 start -->
   <!--  <div class="advert">
        <?php if(isHave($advs[0])){  ?>
            <a href="<?php echo $advs[0]['link_url'] ?>" target="_blank" title="<?php echo $advs[0]['title'] ?>">
                <img src="<?php echo $advs[0]['img_url'] ?>"/>
            </a>
        <?php }else{ ?>
            <a href="javascript:;">
                <img src="<?php echo IMG_PATH ?>index/no_advs.png"/>
            </a>
        <?php } ?>
    </div> -->
    <!-- end -->
    <div class="">
        <ul class="select_wr">
            <li>
                <span>交易身份：</span>
                <div>
                    <a href="<?php echo U('index/search',array('gm'=>$_GET['gm'],'ar'=>$_GET['ar'],'ty'=>$_GET['ty'],'cr'=>'buyer')) ?>" <?php if($cr=='buyer'){  ?> class="on"  <?php } ?>>我要买</a>
                    <a href="<?php echo U('index/search',array('gm'=>$_GET['gm'],'ar'=>$_GET['ar'],'ty'=>$_GET['ty'],'cr'=>'seller')) ?>" <?php if($cr=='seller'){  ?> class="on" <?php } ?>>我要卖</a>
                </div>
            </li>
            <li>
                <span>物品类型：</span>
                <div>
                    <?php foreach ($stdmodes as $k => $v): ?>
                        <a href="<?php echo U('index/search',array('gm'=>$_GET['gm'],'ar'=>$_GET['ar'],'ty'=>$v['abridge'],'cr'=>$_GET['cr'])) ?>" <?php if($v['abridge'] == $ty){  ?> class="on" <?php } ?>><?php echo $v['name'] ?></a>
                    <?php endforeach ?>
                </div>
            </li>
            <?php if($cross){  ?>
                <li>
                    <span>跨区信息：</span>
                    <div>
                        <?php foreach ($cross as $k => $v): ?>
                            <a href="<?php echo U('index/search',array('gm'=>$_GET['gm'],'ar'=>$v['server'],'ty'=>$_GET['ty'],'cr'=>$_GET['cr'])) ?>" <?php if($k == $cross_no){  ?> class="on" <?php } ?>><?php echo $v['name']; ?></a>
                        <?php endforeach ?>
                    </div>
                </li>
                <?php if($servers) ?>
                <li>
                    <span>跨区区服：</span>
                    <div>
                        <?php foreach ($servers as $k => $v): ?>
                            <a href="<?php echo U('index/search',array('gm'=>$_GET['gm'],'ar'=>$v['abridge'],'ty'=>$_GET['ty'],'cr'=>$_GET['cr'])) ?>" <?php if($v['abridge'] == $ar){  ?> class="on" <?php } ?>><?php echo $v['server_name']; ?></a>
                        <?php endforeach ?>
                    </div>
                </li>
            <?php } ?>
            <!-- 价格筛选 -->
            <!-- <li>
                <span>价格筛选：</span>
                <div>
                    <a href="javascript:;">0~100</a>
                    <a href="javascript:;">100~500</a>
                    <a href="javascript:;">500以上</a>
                    <div class="filtrate">
                        <div>
                            <input type="text">
                        </div>
                        -
                        <div>
                            <input type="text">
                        </div>
                        <button>确定</button>
                    </div>
                </div>
            </li> -->
            <!-- 价格晒钱END -->
            <li>
                <span>比价详情：</span>
                <div>
                    <a>
                        <label>
                            <div class="check_box <?php if(isHave($allCheck)){ echo 'selected'; } ?>" onclick="choosePlatformAll(this)" >
                                <input type="checkbox" class="check_all">
                            </div>
                            全部
                        </label>
                    </a>
                    <?php foreach ($platforms as $vo): ?>
                        <a>
                            <label>
                                <div class="check_box <?php if($vo['is_check']){ echo 'selected'; } ?>" onclick="choosePlatform(this)" data-abri="<?php echo $vo['abridge'] ?>">
                                    <input type="checkbox" name="checkPlatform" <?php if($vo['is_check']){ echo 'checked'; } ?>>
                                </div>
                                <?php echo $vo['name'] ?>
                            </label>
                        </a>
                    <?php endforeach ?>
                   <a href="" class="refresh">立即刷新</a>
                </div>
            </li>
        </ul>
        <div class="data_content"></div>
    </div>
</div>
<?php include getTpl('login', 'public'); ?>
<?php include getTpl('footer', 'public');  ?>
<!-- 限制访问次数弹窗 -->
<div>
    <div class="overlay"></div>
    <div class="limit">
        <div class="close limit_close">╳</div>
        <h2>
            <p>亲，您的访问频率过高，当前ip在1分钟内访问次数 <b>已达到<span id="co">0</span>次，最多允许<span id="lim">0</span>次</b> </p>
            <p>如果此限制影响了您的正常使用，您可以选择开通vip获得特权~</p>
        </h2>
        <div>
            <h4>vip特权</h4>
            <div class="privilege_box">
                <div class="privilege_info">
                    <h6>￥100/月</h6>
                    <p>（1分钟限制15次）</p>
                    <div class="level"><img src="<?php echo IMG_PATH ?>mobile/vip.png"><b>1</b></div>
                </div>
                <div class="privilege_info">
                    <h6>￥150/月</h6>
                    <p>（1分钟限制20次）</p>
                    <div class="level"><img src="<?php echo IMG_PATH ?>mobile/vip.png"><b>2</b></div>
                </div>
                <div class="privilege_info">
                    <h6>￥200/月</h6>
                    <p>（1分钟限制30次）</p>
                    <div class="level"><img src="<?php echo IMG_PATH ?>mobile/vip.png"><b>3</b></div>
                </div>
            </div>
            <button><img src="<?php echo IMG_PATH ?>mobile/btn.png"></button>
        </div>
    </div>
</div>
<!-- END -->
<!-- 数据抓取特效 -->
<div class="fetch-wrap" style="display: none;">
    <img src="<?php echo IMG_PATH ?>index/pointer.png" class="pointer">
    <img src="<?php echo IMG_PATH ?>index/fetching.png" class="fetch-text">
    <div class="progress">
      <div class="bar-wrap">
        <?php for ($i=0; $i < count($checkplatforms); $i++) { ?>
          <div class="bar"></div>
        <?php } ?>
      </div>
      <ul class="platforms">
      <?php foreach ($platforms as $po): 
            if($po['is_check']){ 
      ?>
          <li class="<?php echo $po['abridge'] ?>"><?php echo $po['name'] ?> <span>抓取中...</span></li>
      <?php 
            } 
        endforeach ?>
      </ul>
    </div>
</div>
<!-- END -->
<script>
    let interval;
    let progresses = new Array();
    const platforms = <?php echo json_encode($checkplatforms) ?>;
    for (var i = 0; i < platforms.length; i++) {
        progresses.push(1);
    }

    platforms.forEach(platform => getSpiderData(platform));

    //加载数据
    // function getSpiderData() {
    //     var postUrl = "<?php echo U('index/getSpiderData') ?>";
    //     $(".fetch-wrap").show();
    //     $.post(postUrl , $('#postForm').serialize(), function(result){
    //         if(result.status) {
    //             // $(".fetch-wrap").hide();
    //             // $('.data_content').html(result.data);
    //         }else{
    //             $(".fetch-wrap").hide();
    //             $(".overlay").addClass('pop_in');
    //             $(".limit").addClass('pop_in');
    //             $("#co").text(result.data.count);
    //             $("#lim").text(result.data.limit);
    //             var str = "<div class=\"search_none\" style=\"min-height: 300px;\"><img src=\"/statics/default/images/index/icon_03.png\" width=\"149px\" height=\"156px\"/><p>暂无搜索结果！</p></div>";
    //             $('.data_content').html(str);
    //         }
    //     },"json");
    // }

    $(".fetch-wrap").show();  
    function progressing() {
      progresses.forEach((pg, idx) => {
        $('.bar-wrap .bar').eq(idx).css('width', `${pg}%`);
      });
    }

    interval = setInterval(() => {
      progresses = progresses.map(pg => pg >= 25 ? 25 : pg + 1);
      progressing();
    }, 200);

    // progressing();


    function getSpiderData(platform) {
        var postUrl = "<?php echo U('index/getSpiderData') ?>";
        var postForm = $("#postForm");
        $("input[name='platform']").remove();
        var platInput = $("<input type='hidden' name='platform'>");
        platInput.attr('value' , platform);
        postForm.append(platInput);
        if(platform == platforms[platforms.length - 1]) {
            var lastInput = $("<input type='hidden' name='last' value = 1>");
            postForm.append(lastInput);
        }

        $.post(postUrl , postForm.serialize(), function(result){
            if(result.status){
                progresses[platforms.indexOf(platform)] = 25;
                if (progresses.every(pg => pg === 25)) {
                    clearInterval(interval);
                }
                $('.bar-wrap .bar').addClass('done');
                $(`.platforms .${platform}`).addClass('done');
                $(`.platforms .${platform} span`).text('抓取成功');
                progressing();
                if(result.status == 2) {
                    $(".fetch-wrap").hide();
                    $('.data_content').html(result.data);
                }
            }else{
                $(`.platforms .${platform} span`).text(result.info);
            }
        },'json');
        
    }


    function choosePlatform(obj) {
        var selected = 0;
        var abridge  = $(obj).attr('data-abri');
        if($(obj).find("input[type='checkbox']").prop('checked')){
            $(obj).addClass('selected');
            selected = 1;
        }else{
            $(obj).removeClass('selected');
        }
        if($("input[name='checkPlatform']:checked").length == $("input[name='checkPlatform']").length) {
            $(".check_all").parent('.check_box').addClass('selected');
        }else{
            $(".check_all").parent('.check_box').removeClass('selected');
        }

        $.getJSON("<?php echo U('index/setPlatCookie') ?>" , {is_check:selected,abridge:abridge});
    }

    //全选平台
    function choosePlatformAll(obj) {
        var status = $(obj).find("input[type='checkbox']").is(":checked") ? true : false;
        var set = 0;
        if(status) {
            $(".check_box").addClass('selected');
            set = 1;
        }else{
            $(".check_box").removeClass('selected');
        }
        $.getJSON("<?php echo U('index/setPlatAllCookie') ?>", {set:set});
    }
</script>
</body>
</html>